package cn.usts.edu.lesson06;

/**
 * 线程礼让
 * 礼让的线程会重新和竞争的线程再次公平竞争cpu的资源
 * 再次竞争的结果为最后结果
 * */

public class ThreadYieldDemo implements Runnable{

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"开始");
        if (Thread.currentThread().getName().equals("线程A")){ // 如果是线程A就礼让
            Thread.yield();// 线程礼让
        }
        System.out.println(Thread.currentThread().getName()+"执行结束");
    }

    public static void main(String[] args) {
        ThreadYieldDemo threadYieldDemo = new ThreadYieldDemo();
        new Thread(threadYieldDemo,"线程A").start();
        new Thread(threadYieldDemo,"线程B").start();
    }
}
